package com.karry.yl.service.system.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.karry.commons.base.impl.SuperServiceImpl;
import com.karry.commons.vo.PermissionTreeNode;
import com.karry.yl.dao.system.RoleMapper;
import com.karry.yl.dao.system.RoleResourcesMapper;
import com.karry.yl.domain.system.Resources;
import com.karry.yl.domain.system.Role;
import com.karry.yl.domain.system.RoleResources;
import com.karry.yl.service.system.ResourcesService;
import com.karry.yl.service.system.RoleService;
import org.apache.poi.ss.formula.ptg.NotEqualPtg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends SuperServiceImpl<RoleMapper,Role> implements RoleService {

    @Autowired
    private RoleResourcesMapper roleResourcesMapper;
    @Autowired
    private ResourcesService resourcesService;

    @Override
    public void insert(Role role, String[] resourcesCodes) {
        this.insert(role);
        //保存当前角色的权限
        for(String resourcesCode : resourcesCodes){
            RoleResources roleResources = new RoleResources();
            roleResources.setResourcesCode(resourcesCode);
            roleResources.setRoleId(role.getId());
            roleResourcesMapper.insert(roleResources);
        }

    }

    @Override
    public void update(Role role, String[] resourcesCodes) {
        boolean update = this.update(role, new EntityWrapper<Role>().eq("id", role.getId()).eq("orgId", role.getOrgId()));
        if(update){
            //清除旧的权限信息
            roleResourcesMapper.delete(new EntityWrapper<RoleResources>().eq("roleId",role.getId()));
            //保存当前角色的权限
            for(String resourcesCode : resourcesCodes){
                RoleResources roleResources = new RoleResources();
                roleResources.setResourcesCode(resourcesCode);
                roleResources.setRoleId(role.getId());
                roleResourcesMapper.insert(roleResources);
            }
        }
    }

    @Override
    public List<PermissionTreeNode> permissionTree(Long roleId,Integer nature) {
        List<PermissionTreeNode> treeNodes = new ArrayList<>();
        List<Resources> resourcesList =null;
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(3);
        if(nature==1){//公办
                integers.add(1);
                resourcesList = resourcesService.selectList(new EntityWrapper<Resources>().in("status",integers));     //系统中所有的权限
        }else if(nature==2){//民办
                integers.add(2);
                resourcesList = resourcesService.selectList(new EntityWrapper<Resources>().in("status",integers));     //系统中所有的权限
        }

        List<String> rolePermiIdList = resourcesService.findCodeListByRole(roleId);  //该角色已拥有的权限id集合
        if(null != resourcesList) {
            for(Resources resources : resourcesList) {
                String pCode = (resources.getType() == Resources.TYPE_MODULE) ? "0" :(resources.getType() == Resources.TYPE_PAGE ? resources.getParentModule() : resources.getParentPage() );
                PermissionTreeNode treeNode = new PermissionTreeNode(resources.getCode(), resources.getName(), pCode);
                if(null != rolePermiIdList && rolePermiIdList.contains(resources.getCode())) {
                    treeNode.setChecked(Boolean.TRUE);
                }
                treeNodes.add(treeNode);
            }
        }
        return treeNodes;
    }

    @Override
    public void delete(List<Long> ids) {
        for(Long id :ids){
            baseMapper.deleteById(id);
            roleResourcesMapper.delete(new EntityWrapper<RoleResources>().eq("roleId",id));
        }
    }
}
